package org.myself.meeting.controller;

import org.myself.meeting.Utils.Constant;
import org.myself.meeting.model.*;
import org.myself.meeting.service.AwardService;
import org.myself.meeting.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AwardingController {

    @Autowired
    AwardService awardService;

    @Autowired
    EmployeeService employeeService;

    //抽奖系统界面
    @RequestMapping("/awarding")
    @ResponseBody
    public Result chat(@RequestBody Base base){
        Employee emp =employeeService.getEmpsByUsername(base.getUsername()) ;
        AwaedPageRes res = new AwaedPageRes();
        res.setAwardtime(employeeService.getEmpById(emp.getEmployeeid()).getAwardtimes());
        res.setAward(awardService.getAward());

        return new Result(200,"查询成功",res);
    }

    //抽奖记录
    @RequestMapping("/awardrecord")
    @ResponseBody
    public Result awardRecord(@RequestBody AwardRecordAccept acc){
        Employee emp = employeeService.getEmpsByUsername(acc.getUsername());
        if(emp.getAwardtimes() < 1){
            return new Result(400,"false",null);
        }
        Integer result = awardService.addAwardRecord(acc.getAwardname(),emp);
        if(result == 0){
            return new Result(400,"false",null);
        }
        //更新数据库
        result = employeeService.updateAwardtimes(emp.getEmployeeid(),emp.getAwardtimes()-1);
        if(result == 0){
            return new Result(400,"false",null);
        }
        return new Result(200,"success",null);
    }


    //查看抽奖记录/awardshow
    @RequestMapping("/awardshow")
    @ResponseBody
    public Result awardShow(@RequestBody Base base){
        Map<String,List<AwardRecord>> map = new HashMap<String, List<AwardRecord>>();
        map.put("awardrecord",awardService.getAwardRecordAll());
        return new Result(200, "查询成功",map);
    }


    //更改奖品状态/admin/awardapproval
    @RequestMapping("/admin/awardapproval")
    public Result awardApproval(@RequestBody Base base){
        Map<String,List<AwardRecord>> map = new HashMap<String, List<AwardRecord>>();
        map.put("awardrecord",awardService.getAwardRecordStu(Constant.AWARDSTATUSNO));
        return new Result(200, "查询成功",map);
    }

    //奖品发放
    @RequestMapping("/admin/awardrecordupdate")
    public Result awardRecordUpdate(@RequestBody AwardRecordUpdateAcc acc){
        Integer result = awardService.updateAwardById(acc.getAwardrecordid());
        return new Result(200, "查询成功",null);
    }

}
